{% extends "myadmin/base.html" %}

{% block mainbody %}                
    <h3>
        主机信息管理
    </h3>

    <!--编辑主机信息form开始-->
    <form id="edit-profile" action="{% url 'myadmin_hosts_update' host.id %}" class="form-horizontal" method="post" onsubmit="return check(this)" enctype="multipart/form-data">
        {% csrf_token %}
        <fieldset>
            <legend>查看/编辑主机信息</legend>
            <div class="control-group">
                <label class="control-label" for="input02">标签：</label>
                <div class="controls">
                    <input type="text" name="tag" class="input-xlarge" id="input02" value="{{host.tag}}"/>
                </div>
            </div>
            <div class="control-group">
                <label class="control-label" for="input03">IP地址：</label>
                <div class="controls">
                    <input type="text" name="ip" class="input-xlarge" id="input03" value="{{host.ip}}"/>
                </div>
            </div>

            <div class="control-group">
                <label class="control-label" for="input01">主机账号：</label>
                <div class="controls">
                    <input type="text" name="user" class="input-xlarge" id="input01" value="ubuntu" readOnly="true" />
                </div>
            </div>
            <div class="control-group">
                <label class="control-label" for="pass01">登录密码：</label>
                <div class="controls">
                    <input type="password" name="passwd" class="input-xlarge" id="pass01" placeholder="不输入则尝试免密码登录" readOnly="true" />
                </div>
            </div>

            <div class="form-actions">
                <button type="submit" class="btn btn-primary">保存</button> <button type="reset" class="btn">重置</button>
            </div>
        </fieldset>
    </form>
    <!--编辑主机信息form结束-->

    <script type="text/javascript">
        //表单提交前进行数据校验
        function check(form){
            //校验IP地址是否符合规范
            var ip_val = form.ip.value;
            console.log(ip_val);
            if (ip_val.match(/^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}$/) == null){
                alert("IP地址无效，请输入有效的IP地址！");
                form.ip.focus();
                return false;
            }

            if ((ip_val != '{{host.ip}}') && form.user.readOnly){
                alert("更改主机IP地址需要对新的IP重新进行登录验证，请输入主机账号和密码！");
                form.user.readOnly = false;
                form.passwd.readOnly = false;
                form.user.focus();
                return false;
            }
            
            return true;
        }
    </script>

{% endblock %}
